package miniproj.services.data;

import java.io.*;

/**
 * 
 * This class serialize data with the ObjectOutputStream method
 * 
 * @author Baptiste Viale
 * 
 */
public class StreamDataWriter implements DataWriter {

	private String folderPath;

	/**
	 * Instantiates a new stream data writer.
	 * 
	 * @param folderPath
	 *            the folder path
	 */
	public StreamDataWriter(String folderPath) {
		this.folderPath = folderPath;
	}

	/**
	 * Serialize the DataContext using the java object method
	 * 
	 * @param dc
	 *            the data context to be serialized
	 * @param dataContextFileName
	 *            the data context file name
	 */
	@Override
	public void setDataContext(DataContext dc, String dataContextFileName) {
		this.saveObject(dc, folderPath + "/" + dataContextFileName);
	}

	private void saveObject(Object object, String fileFullPath) {
		try {
			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream(fileFullPath));
			oos.writeObject(object);
			oos.close();
			oos.flush();
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFoundException while saving object : "
					+ e.getMessage());
		} catch (IOException e) {
			System.err.println("Input/Output Exception while saving object : "
					+ e.getMessage());
		}
	}
}
